package com.iqiyi.pps.epg.core.rpc.qipu.Composite;

import com.google.common.collect.Lists;
import com.google.gson.Gson;
import knowledge.pub.Metadatas;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: jinlin
 * Date: 15-6-16
 * Time: 上午11:45
 * To change this template use File | Settings | File Templates.
 */
@Component
public class AlternatePlayCollectionImpl {
    private static final Logger logger = LoggerFactory.getLogger(AlternatePlayCollectionImpl.class);
    /**
     备播库结构
     {
     "0": {
     "qipuId": 733440900,
     "displayName": "Wildlife",
     "contentType": 1
     },
     "1": {
     "qipuId": 733600100,
     "duration": 30000,
     "displayName": "wildlife1120",
     "contentType": 1
     },
     "2": {
     "qipuId": 360670000,
     "duration": 86000,
     "displayName": "Ella和Selina互相吐槽 人妻好姐妹",
     "contentType": 1
     }
     }
     */

    public List<Metadatas.MetaSummary> convertToMeta(String jsonStr){
        List<Metadatas.MetaSummary> list = Lists.newArrayList();
        if(StringUtils.isNotBlank(jsonStr)){
        Gson gson = new Gson();
        Map<String,Map> out = gson.fromJson(jsonStr,Map.class);
        int size = out.size();
        for(int i=0; i<size;i++){
            if(out.get(i+"")!=null){
                Map inner = out.get(i+"");
                Metadatas.MetaSummary.Builder metaSu = Metadatas.MetaSummary.newBuilder();
                metaSu.setEntityId(((Double)inner.get("qipuId")).longValue());
                metaSu.setDesc(inner.get("displayName")+"") ;
                list.add(metaSu.build());
            }
        }
        }
        return list;
    }

    public static List<Long> getQipuIds(String jsonStr){
        List<Long> qipuIds = new ArrayList<>();
        if(StringUtils.isNotBlank(jsonStr)){
            Gson gson = new Gson();
            Map<String, Map> out = gson.fromJson(jsonStr, Map.class);
            int size = out.size();
            for(int i=0; i<size;i++){
                if(out.get(i+"")!=null){
                    Map inner = out.get(i+"");
                    qipuIds.add(((Double) inner.get("qipuId")).longValue());
                }
            }
        }

        return qipuIds;
    }

}
